{extend name="public/base"/}
{block name="style"}
<link rel="stylesheet" href="__PUBLIC__/admin/replaceversion/css/version.css?times={:time()}" />
<link rel="stylesheet" type="text/css" href="__PUBLIC__/plugs/webuploader/webuploader.css">
{include file="material/_ali_icon" /}
{/block}
{block name="body"}
<div class="main-box clearfix">
	<header class="main-box-header clearfix">
		<div class="pull-right">
			<button type="button" class="btn btn-primary" id="create">
				<i class="fa fa-plus"></i> 添加 <span class="caret"></span>
			</button>
		</div>
	</header>
	<div class="main-box-body clearfix">
		<table class="table table-hover table-bordered">
			<thead>
				<tr>
					<th width="45px">ID</th>
					<th width="50%">名字</th>
					<th width="20%">替换前</th>
					<th width="20%">替换后</th>
					<th width="120px">操作</th>
				</tr>
			</thead>
			<tbody>
			{empty name="list"}
			<tr><td colspan="9" align="center">暂无数据</td></tr>
			{/empty}
			{volist name="list" id="vo"}
				<tr>
					<td>{$vo.id}</td>
					<td>{$vo.name}</td>
					<td>{$vo.search}</td>
					<td>{$vo.replace}</td>
					<td>
						<button type="button" class="btn btn-primary btn-sm downLoad" data-url="{$vo.file_out}">
							<i style="color:#fff" class="iconfont icon-xiazai-wenjianxiazai-16"></i>
						</button>
					</td>
				</tr>
			{/volist}
			</tbody>
		</table>
	</div>
</div>
<div class="modal fade" id="create-modal">
	<div class="modal-dialog" role="document" style="width:1000px">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">×</span>
				</button>
				<span style="font-size: 20px;"><span id="referralname">添加</span></span>
			</div>
			<div class="modal-body">
				<form class="form modelform">
					<div class="form-group">
						<label class="control-label">版本</label>
						<div class="">
							<div id="picker_ebook" class="picker_button">上传文件</div>
							<input type="hidden" name="file_path" id="file_path">
							<input type="hidden" id="file_name" name="name">
							<span id="fileList_ebook" class="upload-file-list-info">(请上传.txt文件)</span>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label">替换前</label>
						<div class="nameList last">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
							<input type="text" class="form-control" name="search" autocomplete="false">
						</div>
					</div>
					<div class="form-group">
						<label class="control-label" style="display:flex;">
							替换后
							<i class="iconfont icon-tianjia2" st></i>
						</label>
						<div class="next">
							<div class="nameList" id="nameList">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
								<input type="text" class="form-control" name="replace" autocomplete="false" value="">
							</div>
	
						</div>
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="createVersion" class="btn btn-primary ">提交</button>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript" src="__PUBLIC__/plugs/webuploader/webuploader.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/plugs/webuploader/webuploader.custom.js"></script>
<script type="text/javascript" src="__PUBLIC__/plugs/layer/layer.js"></script>
<script>
	
	$(function(){
		$(".icon-tianjia2").click(function () {
			$('#nameList').clone(true).appendTo(".next");
		})
		$('#create').on('click', function () {
			$("#referralname").html("添加");
			
			var $modal = $('#create-modal');
			$modal.on('shown.bs.modal', function () {
				$('#picker_ebook').html('<div>上传文件</div>')
				$("#picker_ebook").SentUploader({
					server: "{:url('Upload/Uploadtxt')}",
					fileNumLimit: 1,
					listName: 'fileList_ebook',
					hiddenName: 'file_path',
					hiddenFileName: 'file_name',
					hiddenValType: 2,
					fileSingleSizeLimit: 50 * 1024 * 1024,
				});
			});	
			$modal.modal({backdrop: 'static', keyboard: false});
		});
		
		$("#createVersion").on('click', function () {
			var url = "{:url('replace')}";
			var data = $('.modelform').serializeJson();
			let newSearch = data.search == '' ? [] : data.search.filter(s => $.trim(s).length > 0)
			let newReplace =  data.replace == '' ? [] : data.replace.filter(s => $.trim(s).length > 0)
			let replace = [];
			if(data.file_path.toString() === ''){
				layer.msg("请上传文件");
				return;
			}
			if(newSearch.length === 0){
				layer.msg("请填写替换前名字");
				return;
			}
			if(newReplace.length == 0){
				layer.msg("请填写替换后名字");
				return;
			}
			for (let i = 0; i < data.replace.length; i += 12) {
				let arr = data.replace.slice(i, i + 12);
				for (let index = 0; index < arr.length; index++) {
					const element = arr[index];
					if(data.search[index] !== '' && element !== '' || data.search[index] === '' && element === '' ){
					}else{
						layer.msg("填写格式错误");
						return;
					}
				}
				replace.push(arr.filter(s => $.trim(s).length > 0).toString())
			}
			data.file_path = data.file_path.toString()
			data.replace = replace;
			data.search = newSearch.toString();
			$.ajax({
				type: "POST",
				url,
				data,
				dataType: "json",
				success: function(data){
					if(data.code==1){
						updateAlert(data.msg, 'success');
						setTimeout(function() {	
							window.location.reload();
						}, 100);
					}else{
						updateAlert("添加，"+data.msg, 'danger');
					}
				},
			})
		});
		$('.downLoad').click(function(){
			let aDom = document.createElement('a');
			aDom.style.display = 'none';
			aDom.href = 'http://'+window.location.host+'/'+$(this).data('url');
			aDom.setAttribute('download', 'name');
			document.body.appendChild(aDom);aDom.click();
			document.body.removeChild(aDom);
		})
	})
</script>
{/block}